const express = require('express')
const multer = require("multer")
const path = require("path")
const app = express();
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');/*post方法*/
const  {recommendTable:secretbase}=require("./database/secretbase");

var allowCors = function (req, res, next) {
  res.header('Access-Control-Allow-Origin', req.headers.origin);
  res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
  res.header('Access-Control-Allow-Credentials', true);
  next();
};

app.use(express.json())
app.use(express.urlencoded({ extended: true }))

app.use(bodyParser.json());// 添加json解析
app.use(bodyParser.urlencoded({extended: false}));

app.use(express.static('pub'))

app.use(allowCors);//使用跨域中间件
app.use(cookieParser("YNAU"));//cookie 签名  

app.post("/login",(req,res,next)=>{
  let  loginkey= {
    username: "admin",
    password: "123123"
}
if (req.body.username===loginkey.username&&req.body.password===loginkey.password) {
  console.log(req.cookies);
  console.log(req.signedCookies);
  res.cookie('ms_username','admin',{httpOnly:true,signed:true}); 
  res.cookie('ms_require','true'); 
  res.send("sussess")
}else{
  res.send("error")
}

})

// app.use(async (req,res,next)=>{
//   let require=false
//   let data=await secretbase.find({secretID:req.body.secretID})
//   require=data.length?true:false;
//   if (req.signedCookies.ms_username=="admin" || require) {
//     next();
//   } else {
//     res.clearCookie("ms_require")
//     res.clearCookie("ms_username")
//     res.send("")
//   }
  
// })


app.use('/',require("./routes/route"));

app.listen(5055, () => {
  console.log('启动');

})


